package com.xdtech.hotel.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.thoughtworks.xstream.XStream;
import com.xdtech.common.annotation.Excel;
import com.xdtech.common.utils.JsonUtil;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.model.Room;
import com.xdtech.hotel.protocol.vo.Ac;
import com.xdtech.hotel.protocol.vo.Card;
import com.xdtech.hotel.protocol.vo.CmdNode;
import com.xdtech.hotel.protocol.vo.Component;
import com.xdtech.hotel.protocol.vo.Dimmer;
import com.xdtech.hotel.protocol.vo.Fault;
import com.xdtech.hotel.protocol.vo.Music;
import com.xdtech.hotel.protocol.vo.Node;
import com.xdtech.hotel.protocol.vo.RoomPanelStatus;
import com.xdtech.hotel.protocol.vo.Scene;
import com.xdtech.hotel.protocol.vo.Tv;
import com.xdtech.hotel.util.HotelConstants;
import com.xdtech.hotel.util.RoomContants;
import com.xdtech.web.freemark.item.GridColumn;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:16:36
 * @since 1.0
 * @see
 */
public class RoomItem extends BaseItem
{
	private Long id;
	@Excel(exportName = "所属区域", exportFieldWidth = 20, exportConvertSign = 0, importConvertSign = 0)
	@GridColumn(title = "所属区域", width = 100, dictionaryCode="SERVICE_AREA")
	private String serviceArea;
	@Excel(exportName = "所在楼层", exportFieldWidth = 10, exportConvertSign = 0, importConvertSign = 0)
	@GridColumn(title = "所在楼层", width = 100, dictionaryCode="ROOM_FLOOR")
	private String floor;
	@Excel(exportName = "房间类型", exportFieldWidth = 20, exportConvertSign = 0, importConvertSign = 0)
	@GridColumn(title = "房间类型", width = 100)
	private String type;
	@Excel(exportName = "房间号", exportFieldWidth = 15, exportConvertSign = 0, importConvertSign = 0)
	@GridColumn(title = "房间号", width = 100)
	private String roomNo;
	@Excel(exportName = "房间状态", exportFieldWidth = 15, exportConvertSign = 0, importConvertSign = 0)
	@GridColumn(title = "房间状态", width = 100, dictionaryCode="ROOM_STATUS")
	private String status;
	@Excel(exportName = "房间名称", exportFieldWidth = 15, exportConvertSign = 0, importConvertSign = 0)
	@GridColumn(title = "房间名称", width = 100)
	private String name;

	private String deviceId;
	@Excel(exportName = "设备名称", exportFieldWidth = 20, exportConvertSign = 0, importConvertSign = 0)
	private String deviceName;
	@Excel(exportName = "设备序列号", exportFieldWidth = 20, exportConvertSign = 0, importConvertSign = 0)
	private String deviceSn;
	@Excel(exportName = "设备IP", exportFieldWidth = 20, exportConvertSign = 0, importConvertSign = 0)
	private String deviceIp;
	@Excel(exportName = "设备通信端口", exportFieldWidth = 20, exportConvertSign = 0, importConvertSign = 0)
	private String devicePort;

	private String configInfo;
	
	private String customer;
	
	private Map<String, String> operationStatus = new ConcurrentHashMap<String, String>();
	
	private String scene = "";

	private List<DeviceLinkItem> deviceLinks = new ArrayList<DeviceLinkItem>();

	public void setId(Long id)
	{
		this.id = id;
	}

	public Long getId()
	{
		return id;
	}

	public void setRoomNo(String roomNo)
	{
		this.roomNo = roomNo;
		addQuerys("roomNo", roomNo);
	}

	public String getRoomNo()
	{
		return roomNo;
	}

	public void setStatus(String status)
	{
		this.status = status;
		addQuerys("status", status);
	}

	public String getStatus()
	{
		return status;
	}

	public void setName(String name)
	{
		this.name = name;
		addQuerys("name", name);
	}

	public String getName()
	{
		return name;
	}

	public void setType(String type)
	{
		this.type = type;
		addQuerys("type", type);
	}

	public String getType()
	{
		return type;
	}

	public void setServiceArea(String serviceArea)
	{
		this.serviceArea = serviceArea;
		addQuerys("serviceArea", serviceArea);
	}

	public String getServiceArea()
	{
		return serviceArea;
	}

	public String getDeviceId()
	{
		return deviceId;
	}

	public void setDeviceId(String deviceId)
	{
		this.deviceId = deviceId;
	}

	public String getDeviceName()
	{
		return deviceName;
	}

	public void setDeviceName(String deviceName)
	{
		this.deviceName = deviceName;
	}

	public List<DeviceLinkItem> getDeviceLinks()
	{
		return deviceLinks;
	}

	public void setDeviceLinks(List<DeviceLinkItem> deviceLinks)
	{
		this.deviceLinks = deviceLinks;
	}

	public String getConfigInfo()
	{
		return configInfo;
	}

	public void setConfigInfo(String configInfo)
	{
		this.configInfo = configInfo;
	}

	public String getDeviceSn()
	{
		return deviceSn;
	}

	public void setDeviceSn(String deviceSn)
	{
		this.deviceSn = deviceSn;
	}

	public String getDeviceIp()
	{
		return deviceIp;
	}

	public void setDeviceIp(String deviceIp)
	{
		this.deviceIp = deviceIp;
	}

	public String getDevicePort()
	{
		return devicePort;
	}

	public void setDevicePort(String devicePort)
	{
		this.devicePort = devicePort;
	}

	public String getFloor()
	{
		return floor;
	}

	public void setFloor(String floor)
	{
		this.floor = floor;
		addQuerys("floor", floor);
	}

	public String getCustomer() {
		return customer;
	}

	public void setCustomer(String customer) {
		this.customer = customer;
	}

	
	
	public Map<String, String> getOperationStatus() {
		return operationStatus;
	}

	public void setOperationStatus(Map<String, String> operationStatus) {
		this.operationStatus = operationStatus;
	}

	public String getScene() {
		return scene;
	}

	public void setScene(String scene) {
		this.scene = scene;
	}

	/**
	 * 根据model构建vo
	 * 
	 * @author max
	 * @return
	 */
	public static RoomItem createItem(Room room)
	{

		if (room == null)
		{
			return null;
		}
		RoomItem roomItem = (RoomItem) new XStream().fromXML(room.getConfigXmlInfo());
		roomItem.setId(room.getId());
		//		RoomItem roomItem = new RoomItem();
		//		BeanUtils.copyProperties(roomItem, room);
		//		//自定义属性设置填充
		//		roomItem.setDeviceId(room.getDevice().getId().toString());
		//		roomItem.setDeviceName(room.getDevice().getName());
		//		roomItem.set
		return roomItem;
	}

	/**
	 * 根据model集合创建vo集合
	 * 
	 * @author max
	 * @return
	 */
	public static List<RoomItem> createItems(List<Room> rooms)
	{
		List<RoomItem> roomItems = new ArrayList<RoomItem>();
		for (Room room : rooms)
		{
			roomItems.add(createItem(room));
		}
		return roomItems;
	}

	

	/**
	 * 
	 * @author max.zheng
	 * @create 2015-5-5下午10:50:34
	 * @modified by
	 * @param faults
	 */
	public boolean addFault(Fault fault) {
//		for (DeviceLinkItem deviceLinkItem : deviceLinks)
//		{
//			String ltype = deviceLinkItem.getLtype();
//			if (HotelConstants.COMPONENT_MUSIC.equals(ltype))
//			{
//				//音乐
//				component = new Music(componentJson.getString("addr"),componentJson.getString("channel"),componentJson.getString("power"),componentJson.getString("volume"),componentJson.getString("input"),componentJson.getString("ch"),componentJson.getString("item"));
//			} else if (HotelConstants.COMPONENT_TV.equals(ltype))
//			{
//				//电视
//				component = new Tv(componentJson.getString("addr"),componentJson.getString("channel"),componentJson.getString("power"),componentJson.getString("volume"),componentJson.getString("input"),componentJson.getString("ch"),componentJson.getString("item"));
//			} else if (HotelConstants.COMPONENT_AC.equals(ltype))
//			{
//				//空调
//				component = new Ac(componentJson.getString("addr"),componentJson.getString("channel"),componentJson.getString("power"),componentJson.getString("mode"),componentJson.getString("fan"),componentJson.getString("temp"));
//			} else if (HotelConstants.COMPONENT_DIMMER.equals(ltype))
//			{
//				//电灯
//				component = new Dimmer(componentJson.getString("addr"),componentJson.getString("channel"),componentJson.getString("status"),componentJson.getString("freq"),componentJson.getString("delay"),componentJson.getString("value"));
//			}
//		}
		//设置成故障
		this.status = HotelConstants.ROOM_MAINTAIN_STATUS;
		return true;
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-5-5 下午4:13:28
	 * @param card
	 */
	public boolean updateCard(Card card)
	{
		return true;
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-5-5 下午4:13:25
	 * @param scene
	 */
	public boolean updateScene(Scene scene)
	{
//		for (DeviceLinkItem deviceLinkItem : deviceLinks)
//		{
//
//		}
		this.scene = scene.getNumber();
		return true;
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-5-5 下午4:13:21
	 * @param music
	 */
	public boolean updateMusic(Music music)
	{
		for (DeviceLinkItem deviceLinkItem : deviceLinks)
		{
			if (HotelConstants.COMPONENT_MUSIC.equals(deviceLinkItem.getLtype()))
			{
				Music musicTemp = (Music)new XStream().fromXML(deviceLinkItem.getComponent());
				if (null != musicTemp&&musicTemp.getAddr().equals(music.getAddr()))
				{
					deviceLinkItem.setComponent(new XStream().toXML(music));
					return true;
				}
				
			}
		}
		return false;
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-5-5 下午4:13:16
	 * @param dimmer
	 */
	public boolean updateDimmer(Dimmer dimmer)
	{
		for (DeviceLinkItem deviceLinkItem : deviceLinks)
		{
			if (HotelConstants.COMPONENT_DIMMER.equals(deviceLinkItem.getLtype()))
			{
				Dimmer dimmerTemp = (Dimmer)new XStream().fromXML(deviceLinkItem.getComponent());
				if (null != dimmerTemp&&dimmerTemp.getAddr().equals(dimmerTemp.getAddr()))
				{
					deviceLinkItem.setComponent(new XStream().toXML(dimmer));
					return true;
				}
				
			}
		}
		return false;
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-5-5 下午4:13:11
	 * @param ac
	 */
	public boolean updateAc(Ac ac)
	{
		for (DeviceLinkItem deviceLinkItem : deviceLinks)
		{
			if (HotelConstants.COMPONENT_AC.equals(deviceLinkItem.getLtype()))
			{
				Ac acTemp = (Ac)new XStream().fromXML(deviceLinkItem.getComponent());
				if (null != acTemp&&acTemp.getAddr().equals(acTemp.getAddr()))
				{
					deviceLinkItem.setComponent(new XStream().toXML(ac));
					return true;
				}
				
			}
		}
		return false;
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-5-5 下午4:13:06
	 * @param node
	 */
	public boolean updateNode(CmdNode node)
	{
		for (DeviceLinkItem deviceLinkItem : deviceLinks)
		{
			if (HotelConstants.COMPONENT_NODE.equals(deviceLinkItem.getLtype()))
			{
				Node nodeTemp = (Node)new XStream().fromXML(deviceLinkItem.getComponent());
				if (null != nodeTemp&&nodeTemp.getAddr().equals(node.getAddr()))
				{
					//节点类型
					nodeTemp.setAddr(node.getAddr());
					nodeTemp.setChannel(node.getChannel());
					nodeTemp.setDelay(node.getDelay());
					nodeTemp.setFreq(node.getFreq());
					nodeTemp.setStatus(node.getStatus());
					deviceLinkItem.setComponent(new XStream().toXML(nodeTemp));
					return true;
				}
				
			}
		}
		return false;
	}

	/**
	 * 
	 * @author max.zheng
	 * @since 2015-5-5 下午4:09:43
	 * @param tv
	 */
	public boolean updateTv(Tv tv)
	{
		for (DeviceLinkItem deviceLinkItem : deviceLinks)
		{
			if (HotelConstants.COMPONENT_TV.equals(deviceLinkItem.getLtype()))
			{
				Tv tvTemp = (Tv)new XStream().fromXML(deviceLinkItem.getComponent());
				if (null != tvTemp&&tvTemp.getAddr().equals(tvTemp.getAddr()))
				{
					deviceLinkItem.setComponent(new XStream().toXML(tv));
					return true;
				}
				
			}
		}
		return false;
	}

	/**
	 * 
	 * @author <a href="max.zheng@zkteco.com">郑志雄</>
	 * @since 2015-5-7 上午9:41:00
	 * @param rs
	 * @return
	 */
	public boolean updateRoomStatus(RoomPanelStatus rs)
	{
//		请求清扫 1 取消清扫 2
//		请求退房 3 取消退房 4 
//		请求送餐 5
//		取消送餐 6
//		请求洗衣 7
//		取消洗衣 8
//		请求搬运 9
//		取消搬运 10
//		请求勿扰 11
//		取消勿扰 12
//		顾客不满 13
//		取消不满 14
//		顾客报警 15
//		取消报警 16
		//设置操作状态
		int number = Integer.valueOf(rs.getNumber());
		if (number%2==0) {
			this.operationStatus.remove((number-1)+"");
		}else {
			this.operationStatus.put(rs.getNumber(), RoomContants.roomPanelOperation.get(rs.getNumber()));
		}
		
		//变更状态
		if (HotelConstants.ROOM_PANEL_OPERATION_1.equals(rs.getNumber()))
		{
			//请求清扫，就是脏空
			this.status = HotelConstants.ROOM_WAIT_CLEAR_STATUS;
		}
		if (HotelConstants.ROOM_PANEL_OPERATION_2.equals(rs.getNumber()))
		{
			//取消清扫，就是干净
			this.status = HotelConstants.ROOM_CLEAN_STATUS;
		}
		if (HotelConstants.ROOM_PANEL_OPERATION_3.equals(rs.getNumber()))
		{
//			请求退房 3 取消退房 4 
			this.status = HotelConstants.ROOM_OUT_STATUS;
		}
		if (HotelConstants.ROOM_PANEL_OPERATION_15.equals(rs.getNumber()))
		{
			this.status = HotelConstants.ROOM_MAINTAIN_STATUS;
		}
		if (HotelConstants.ROOM_PANEL_OPERATION_16.equals(rs.getNumber()))
		{
			this.status = HotelConstants.ROOM_WORK_OFF_STATUS;
		}
//		this.status = rs.getNumber();
		return true;
	}

}
